import struct
from data_type_yizimi import *
import os

def get_pcap_mode():
    Pcapheader_mode = msg_mode(0xFFFFFFFF, "Pcapheader")
    Pcapheader_mode.add_mode_seg(4, 'Magic')
    Pcapheader_mode.add_mode_seg(2, 'Major')
    Pcapheader_mode.add_mode_seg(2, 'Minor')
    Pcapheader_mode.add_mode_seg(4, 'ThisZone')
    Pcapheader_mode.add_mode_seg(4, 'SigFigs')
    Pcapheader_mode.add_mode_seg(4, 'SnapLen')
    Pcapheader_mode.add_mode_seg(4, 'LinkType')
    return Pcapheader_mode

# class Pcapheader(msg_struct):
    
#     def __init__(self, len: int):
#         super(Pcapheader, self).__init__(len, 0xFFFFFFFF)


def read_bin(length: int, data: bytes):
    # f = open("openflow6.pcap", "rb")
    int_size = struct.calcsize("c")
    get_bin = []
    # print(int_size)
    for i in range(int(length / int_size)):
        # print(data[:int_size])
        (i,), data = struct.unpack("c", data[:int_size]), data[int_size:]
        # print(hex(ord(i)), data)
        get_bin.append(ord(i))
    return get_bin, data        
            
if __name__ == "__main__":
    print(os.path.getsize("./openflow6.pcap"))
    length = os.path.getsize("./openflow6.pcap")
    f = open("openflow6.pcap", "rb")
    pcapheader = 0
    data = f.read(length)
    # print(type(data))
    pm = get_pcap_mode()
    # ph = Pcapheader(24)
    msg, data = read_bin(length, data)
    print(msg, data)
    ph = iden_message(msg, [pm])
    ph.Print()